﻿package igz.fleaxy.util 
{
	import igz.fleaxy.util.StringTrimType;
	
	/**
	 * ...
	 * @author Pedro Sánchez Aparicio - pedro.sanchez@intelygenz.com [www.Intelygenz.com]
	 */
	public class StringUtil 
	{

		/**
		 *  Verifica si una cadena cumple el patrón establecido
		 * 
		 * @param	p_string 	  String
		 * @param	p_expRegType  StringTrimType
		 * 
		 * @example $IsValidText( "pedro.sanchez@intelygenz.com", ExpRegType.EMAIL);
		 * 
		 */
		static public function $Validate ( p_string:String, p_expRegType:RegExp ) : Boolean {
			var expReg:RegExp = p_expRegType;
			return expReg.test( Convert.$ToString( p_string ) );
		}
			
		/**
		 *  Elimina espacios en blanco de una cadena
		 * 
		 * @param	p_string 	String
		 * @param	p_trimType  StringTrimType
		 * @param   p_flags		String  
		 * 
		 * @example nuevaCadena = Trim( cadenaOriginal, StringTrimType.TRIM_LEFT, "g");
		 * 
		 */
		static public function $Trim(p_string:String, p_trimType:uint = 1, p_flags:String = "g" ) : String {
			var re:RegExp;

			if (p_string == null) return "";

			switch ( p_trimType ) {
				case StringTrimType.TRIM_LEFT :
					re = new RegExp("^\\s*", p_flags);
					p_string = p_string.replace(re, "");
					break;
				case StringTrimType.TRIM_RIGHT :
					re = new RegExp("\\s*$", p_flags);
					p_string = p_string.replace(re, "");
					break;
				case StringTrimType.TRIM_ALL :
					re = new RegExp("\\s*\\s", p_flags);
					p_string = p_string.replace(re, "");
				case StringTrimType.TRIM_LEFT_RIGHT :
					re = new RegExp("^\\s*", p_flags); //left
					var re2:RegExp= new RegExp("\\s*$", p_flags); //Right
					p_string = p_string.replace(re, "").replace(re2, "");
					break;
			}
			return p_string;
		}
		
		/**
		 *  Elimina las etiquetas html de una cadena
		 * 
		 * @param	value 	String
		 * 
		 * @example $RemoveHtmlTags( "<h1>Titulo</h1>" );
		 * 
		 */
		static public function $RemoveHtmlTags( value:String ):String{
			return (value != null) ? value.replace(/<.*?>/g, "") : "";			
		}

		
		/**
		 *  Convierte las entidades HTML de una cadena en sus caracteres correspondientes 
		 * 
		 *  Para no ralentizar el proceso, por defecto solo sustituye las más habituales
		 * 	Usar p_allReplaces = true para sustituir todas
		 * 
		 * @param	p_text 	String
		 * @param	p_allReplaces	String
		 * 
		 * @example $ReplaceHtmlEntities( "&#171;Look &amp; Feel&#187;" ); -> «Look & Feel»
		 * 
		 */
		static public function $ReplaceHtmlEntities( p_text: String, p_allReplaces: Boolean = false ) : String {
			var text:String = p_text.replace( /\&quot;/g, "\"" );
			text = text.replace( /\&euro;/g, "€" );
			text = text.replace( /\&apos;/g, "'" );
			text = text.replace( /\&amp;/g, "&" );
			text = text.replace( /\&lt;/g, "<" );
			text = text.replace( /\&gt;/g, ">" );
			text = text.replace( /\&uuml;/g, "ü" );
			text = text.replace( /\&Aacute;/g, "Á" );
			text = text.replace( /\&aacute;/g, "á" );
			text = text.replace( /\&Eacute;/g, "É" );
			text = text.replace( /\&eacute;/g, "é" );
			text = text.replace( /\&Iacute;/g, "Í" );
			text = text.replace( /\&iacute;/g, "í" );
			text = text.replace( /\&Oacute;/g, "Ó" );
			text = text.replace( /\&oacute;/g, "ó" );
			text = text.replace( /\&Uacute;/g, "Ú" );
			text = text.replace( /\&Uuml;/g, "Ü" );
			text = text.replace( /\&uacute;/g, "ú" );
			text = text.replace( /\&uuml;/g, "ü" );
			text = text.replace( /\&Ntilde;/g, "Ñ" );
			text = text.replace( /\&ntilde;/g, "ñ" );
			text = text.replace( /\&Ccedil;/g, "Ç" );
			text = text.replace( /\&ccedil;/g, "ç" );
			text = text.replace( /\&nbsp;/g, " " );
			text = text.replace( /\&iexcl;/g, "¡" );
			text = text.replace( /\&iquest;/g, "¿" );
			
			
			if ( p_allReplaces ) {

				text = text.replace( /\&cent;/g, "¢" );
				text = text.replace( /\&pound;/g, "£" );
				text = text.replace( /\&curren;/g, "¤" );
				text = text.replace( /\&yen;/g, "¥" );
				text = text.replace( /\&brvbar;/g, "¦" );
				text = text.replace( /\&sect;/g, "§" );
				text = text.replace( /\&uml;/g, "¨" );
				text = text.replace( /\&copy;/g, "©" );
				text = text.replace( /\&ordf;/g, "ª" );
				text = text.replace( /\&laquo;/g, "«" );
				text = text.replace( /\&not;/g, "¬" );
				text = text.replace( /\&shy;/g, "­" );
				text = text.replace( /\&reg;/g, "®" );
				text = text.replace( /\&macr;/g, "¯" );
				text = text.replace( /\&deg;/g, "°" );
				text = text.replace( /\&plusmn;/g, "±" );
				text = text.replace( /\&sup2;/g, "²" );
				text = text.replace( /\&sup3;/g, "³" );
				text = text.replace( /\&acute;/g, "´" );
				text = text.replace( /\&micro;/g, "µ" );
				text = text.replace( /\&para;/g, "¶" );
				text = text.replace( /\&middot;/g, "·" );
				text = text.replace( /\&cedil;/g, "¸" );
				text = text.replace( /\&sup1;/g, "¹" );
				text = text.replace( /\&ordm;/g, "º" );
				text = text.replace( /\&raquo;/g, "»" );
				text = text.replace( /\&frac14;/g, "¼" );
				text = text.replace( /\&frac12;/g, "½" );
				text = text.replace( /\&frac34;/g, "¾" );
				text = text.replace( /\&Agrave;/g, "À" );
				text = text.replace( /\&Acirc;/g, "Â" );
				text = text.replace( /\&Atilde;/g, "Ã" );
				text = text.replace( /\&Auml;/g, "Ä" );
				text = text.replace( /\&Aring;/g, "Å" );
				text = text.replace( /\&AElig;/g, "Æ" );
				text = text.replace( /\&Egrave;/g, "È" );
				text = text.replace( /\&Ecirc;/g, "Ê" );
				text = text.replace( /\&Euml;/g, "Ë" );
				text = text.replace( /\&Igrave;/g, "Ì" );
				text = text.replace( /\&Icirc;/g, "Î" );
				text = text.replace( /\&Iuml;/g, "Ï" );
				text = text.replace( /\&ETH;/g, "Ð" );
				text = text.replace( /\&Ograve;/g, "Ò" );
				text = text.replace( /\&Ocirc;/g, "Ô" );
				text = text.replace( /\&Otilde;/g, "Õ" );
				text = text.replace( /\&Ouml;/g, "Ö" );
				text = text.replace( /\&times;/g, "×" );
				text = text.replace( /\&Oslash;/g, "Ø" );
				text = text.replace( /\&Ugrave;/g, "Ù" );
				text = text.replace( /\&Ucirc;/g, "Û" );
				text = text.replace( /\&Yacute;/g, "Ý" );
				text = text.replace( /\&THORN;/g, "Þ" );
				text = text.replace( /\&szlig;/g, "ß" );
				text = text.replace( /\&agrave;/g, "à" );
				text = text.replace( /\&acirc;/g, "â" );
				text = text.replace( /\&atilde;/g, "ã" );
				text = text.replace( /\&auml;/g, "ä" );
				text = text.replace( /\&aring;/g, "å" );
				text = text.replace( /\&aelig;/g, "æ" );
				text = text.replace( /\&egrave;/g, "è" );
				text = text.replace( /\&ecirc;/g, "ê" );
				text = text.replace( /\&euml;/g, "ë" );
				text = text.replace( /\&igrave;/g, "ì" );
				text = text.replace( /\&iacute;/g, "í" );
				text = text.replace( /\&icirc;/g, "î" );
				text = text.replace( /\&iuml;/g, "ï" );
				text = text.replace( /\&eth;/g, "ð" );
				text = text.replace( /\&ograve;/g, "ò" );
				text = text.replace( /\&ocirc;/g, "ô" );
				text = text.replace( /\&otilde;/g, "õ" );
				text = text.replace( /\&ouml;/g, "ö" );
				text = text.replace( /\&divide;/g, "÷" );
				text = text.replace( /\&oslash;/g, "ø" );
				text = text.replace( /\&ugrave;/g, "ù" );
				text = text.replace( /\&ucirc;/g, "û" );
				text = text.replace( /\&yacute;/g, "ý" );
				text = text.replace( /\&thorn;/g, "þ" );
				text = text.replace( /\&yuml;/g, "ÿ" );
			}

			text = text.replace( /&#([0-9]+?);/g, _ReplaceHtmlEntities_GetCharFromCharCode );
			return text;
		}
		static private function _ReplaceHtmlEntities_GetCharFromCharCode():String {
			return String.fromCharCode( new Number(arguments[1]) );
		}
		
		/**
		 *  Elimina las vocales acentuadas de una cadena de texto
		 * 
		 * @param	value 	String
		 * 
		 * @example 
		 * 
		 */
		static public function $RemoveAccent ( value:String ) : String {
			var accentLess : String;
			
			accentLess = value.replace("á", "a");
			accentLess = accentLess.replace("é", "e");
			accentLess = accentLess.replace("í", "i");
			accentLess = accentLess.replace("ó", "o");
			accentLess = accentLess.replace("ú", "u");
			accentLess = accentLess.replace("ü", "u");
			
			return accentLess;
		}
		
		/**
		 *  Convierte la primera letra de la palabra en Mayúsculas y el resto en Minúsculas
		 *  para todas las palabras del texto indicado.
		 * 
		 * @param	p_text The string text
		 */
		static public function $Capitalize ( p_text:String ) : String {
			var capitalizedText : String = "";
			
			var textSplit : Array = p_text.split(" ");
			var word : String;
			for each ( word in textSplit ) {
				capitalizedText += word.substr(0, 1).toUpperCase() + word.substr(1).toLowerCase() + " ";
			}
			
			if ( textSplit.length > 0 ) {
				capitalizedText = capitalizedText.substr( 0, capitalizedText.length - 1 );
			}
			return capitalizedText;
		}
		
		static public function $IsNullOrEmpty ( p_value:String ) : Boolean {
			return p_value == null || p_value == "";
		}
		
		public function StringUtil() { }
		
	}
	
}